//
//  AppUtils.m
//  EcmDoctor
//
//  Created by 张进 on 15/7/6.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "AppUtils.h"
#import "NetWorkUtil.h"

@implementation AppUtils

static UIFont *fontSTLibian17 = nil;

//获取屏幕尺寸
+(CGRect)getScreenBounds{
    CGRect rx = [ UIScreen mainScreen ].bounds;
    return rx;
}

//本应用导航栏标题（自定义字体）
+(UILabel *)generateNavigationItemTitleViewWithTitle:(NSString *)text{
    
    UIFont *font = [UIFont fontWithName:@"STLibian-SC-Regular" size:25];
    CGSize theStringSize =[text sizeWithFont:font constrainedToSize:CGSizeMake(999, 2000) lineBreakMode:NSLineBreakByTruncatingTail];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, theStringSize.width, 25)];
    label.font = font;
    label.text = text;
    label.textColor = [UIColor whiteColor];
    
    
    
    return label;
}

+(UIBarButtonItem *)generateNavigationBarButtonWithTitle:(NSString *)text withTarget:(id)target action:(SEL)selector{
    UIFont *font = [UIFont fontWithName:@"STLibian-SC-Regular" size:23];

    if ([text hasPrefix:@"治疗方案"] || [text isEqualToString:@"详情"]) {
        font = [UIFont fontWithName:@"STLibian-SC-Regular" size:20];
    }
    
    CGSize theStringSize =[text sizeWithFont:font constrainedToSize:CGSizeMake(999, 2000) lineBreakMode:NSLineBreakByTruncatingTail];
    
    CGRect textRect = CGRectMake(0, 0, theStringSize.width, 25);
    
    UILabel *label = [[UILabel alloc] initWithFrame:textRect];
    label.font = font;
    label.text = text;
    label.textColor = [UIColor whiteColor];
    
//    UIGraphicsBeginImageContext(textRect.size);
    UIGraphicsBeginImageContextWithOptions(textRect.size, NO, 3.0);
    
    [label drawTextInRect:textRect];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:resultingImage style:UIBarButtonItemStylePlain target:target action:selector];
    item.tintColor = [UIColor whiteColor];
    return item;
}

//获取应用自定义字体17号
+(UIFont *)getCustomFontWithSize17{
    
    if (fontSTLibian17 == nil) {
        fontSTLibian17 = [UIFont fontWithName:@"STLibian-SC-Regular" size:17];
    }
    
    return fontSTLibian17;
}
//获取应用自定义字体
+(UIFont *)getCustomFontWithSize:(CGFloat)size{
    UIFont *font = [UIFont fontWithName:@"STLibian-SC-Regular" size:size];
    return font;
}

//App服务自定义名称
+(NSString *)appServiceName{
    return @"EcmDoctor-1234509876FEDCBA";
}

+(NSString *)tokenKey{
    return @"EcmDoctor-UserToken";
}

//判断字符串是否为空
+ (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+(UIColor *)getBackground{
    CGRect rect = [UIScreen mainScreen].bounds;
    
    UIColor *color = [UIColor colorWithRed:235/255.0 green:215/255.0 blue:190/255.0 alpha:1.0];
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = [UIImage imageNamed:@"花纹"];
//    CGFloat height =  image.size.height;
    [image drawInRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:resultingImage];
}

+(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)targetSize{
    
    UIGraphicsBeginImageContext(targetSize);

    [img drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}

+(UIImage *)mergeImage:(UIImage *)fromImg toImageCenter:(UIImage *)toImg {
    
    CGSize targetSize = toImg.size;
    
    UIGraphicsBeginImageContext(targetSize);
    
//    [toImg drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
    
    CGSize fromSize = fromImg.size;
    CGFloat fromX = (targetSize.width - fromSize.width)/2.0;;
//    if (targetSize.width > fromSize.width) {
//        fromX = (targetSize.width - fromSize.width)/2.0;
//    }else{
//        fromX = (fromSize.width - targetSize.width)/2.0;
//    }
    CGFloat fromY = (targetSize.height - fromSize.height)/2.0;
//    if (targetSize.height > fromSize.height) {
//        fromY = (targetSize.height - fromSize.height)/2.0;
//    }else{
//        fromY = (fromSize.height - targetSize.height)/2.0;
//    }
    
//    [fromImg drawInRect:CGRectMake(fromX, fromY, fromSize.width, fromSize.height)];
//    
//    CGPoint pointImg2 = cropRect.origin;
    
//    fromX = (int)fromX;
//    fromY = (int)fromY;
    
    [fromImg drawAtPoint: CGPointMake(fromX, fromY)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}


+(UIImage*)getSubImage:(UIImage*)img withRect:(CGRect)rect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(img.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    
    return smallImage;
}

+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

+ (void)hideKeyBoard
{
    for (UIWindow* window in [UIApplication sharedApplication].windows)
    {
        for (UIView* view in window.subviews)
        {
            [self dismissAllKeyBoardInView:view];
        }
    }
}

+(BOOL) dismissAllKeyBoardInView:(UIView *)view
{
    if([view isFirstResponder])
    {
        [view resignFirstResponder];
        return YES;
    }
    for(UIView *subView in view.subviews)
    {
        if([self dismissAllKeyBoardInView:subView])
        {
            return YES;
        }
    }
    return NO;
}

+ (NSString *)weekForClinic:(NSString *)week {
    
    if ([week isEqualToString:@"1"]) {
        week = @"星期一";
    } else if ([week isEqualToString:@"2"]) {
        week = @"星期二";
    } else if ([week isEqualToString:@"3"]) {
        week = @"星期三";
    } else if ([week isEqualToString:@"4"]) {
        week = @"星期四";
    } else if ([week isEqualToString:@"5"]) {
        week = @"星期五";
    } else if ([week isEqualToString:@"6"]) {
        week = @"星期六";
    } else if ([week isEqualToString:@"7"]) {
        week = @"星期日";
    }
    
    return week;
}


@end
